package com.zhijiaoyun.controller.api;

import com.zhijiaoyun.common.CommonUtils;
import com.zhijiaoyun.common.DsConstant;
import com.zhijiaoyun.controller.base.BaseController;
import com.zhijiaoyun.model.News;
import com.zhijiaoyun.model.NewsView;
import com.zhijiaoyun.model.dto.NewsRecommendDto;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shisfish
 * @date 2017/6/6
 */
@Controller
@RequestMapping(value = "/api/news", produces = "application/json; charset=utf-8")
public class NewsApi extends BaseController {

    /**
     * 资讯列表
     *
     * @param pageIndex
     * @param pageSize
     * @param title
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/newsList", method = RequestMethod.GET)
    public Map<String, Object> newsList(int pageIndex, int pageSize, String title) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_DEFAULT;
        }
        return newsService.pageNews(pageIndex, pageSize, title);
    }

    /**
     * 热点资讯
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/newsHot", method = RequestMethod.GET)
    public Map<String, Object> newsHot(int pageIndex, int pageSize) {
        if (pageIndex <= 0) {
            pageIndex = 1;
        }
        if (pageSize <= 0) {
            pageSize = DsConstant.PAGESIZE_FOUR;
        }
        return newsService.pageNewsHot(pageIndex, pageSize);
    }

    @ResponseBody
    @RequestMapping(value = "/getRecommend", method = RequestMethod.GET)
    public Map<String, Object> getRecommend() {
        Map<String, Object> map = new HashedMap<>();

        // 找到所有的推荐课程
        List<NewsRecommendDto> news = newsRecommendService.getNewsRecommentByParamsDto(1, 5);

        map.put("list", news);
        return map;
    }

    /**
     * 资讯详情
     *
     * @param newsId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/{newsId}/newsInfo", method = RequestMethod.GET)
    public Map<String, Object> newsDetail(@PathVariable("newsId") long newsId) {
        return newsService.getNewsById(newsId);
    }

    @ResponseBody
    @RequestMapping(value = "/{newsId}/view", method = RequestMethod.POST)
    public Map<String, Object> view(@PathVariable("newsId") Long newsId, String token) {
        Map<String, Object> map = new HashMap<>();
        Long userId = CommonUtils.getUserIdByToken(token);
        if (userId == 0) {
            map.put(DsConstant.CODE, DsConstant.CODE_TOKEN_LOST);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_TOKEN_LOST);
            return map;
        }
        News news = newsService.getNewsByNewsId(newsId);
        if (news == null) {
            map.put(DsConstant.CODE, DsConstant.CODE_NEWS_LOST);
            map.put(DsConstant.MESSAGE, DsConstant.MESSAGE_NEWS_LOST);
            return map;
        }
        NewsView view = new NewsView(newsId, 0, userId, "");
        newsViewService.addNewsView(view);

        news.setViewNum(news.getViewNum() + 1);
        newsService.modifyNews(news);
        map.put(DsConstant.CODE, DsConstant.CODE_SUCCESS);
        return map;
    }


}
